{include file="common/head"}
<head>
  <title>充值设置</title>

  <style type="text/css">
      #top{background: initial;border-bottom:1px solid rgba(0,0,0,0.1);max-width: 800px;}
    .mb-3{margin-bottom:0!important;}
  </style>    
</head><body style="max-width: 800px;">
  <div id="header"></div>
  <div id="top" class="row text-center" style="height: 50px;line-height: 50px;">
    <div class="col-2 text-center"></div>
    <div class="col-8 text-center">充值设置</div>
    <div class="col-2 text"></div>
  </div>

    <div class="container p-5 my-2 d-grid" style="padding: 0 10px;">
      <table class="table table-sm table-striped">
        <thead class="table-info">
          <th>序号</th>
          <th>充值金额</th>
          <th>赠送金额</th>

        </thead>
        <tbody>
          {foreach $data as $k=>$log}
          <tr class="charge_item"> 
            <td class="sn">{$k+1}</td>
            <td><input type="number" class="money form-control" value="{$log['money']}"  min="1" /></td>
            <td><input type="number" class="gift form-control" value="{$log['money_gift']}" min="1" /></td>
          </tr>
          {/foreach}


        </tbody>
      </table>

      <button class="btn btn-primary" id="new_item">
        <span class="bi-plus">新增设置项</span>
      </button>
      <br/>

      <button type="button" class="btn btn-info btn-block" id="save">保存设定(Save)</button>      
    </div>

<script type="text/javascript">
$(document).ready(function(){

  $(".money,.gift").on('focus',function(){
    $(this).css({"backgroundColor":"#f60"})
  })
  $(".money,.gift").on('blur',function(){
    $(this).css({"backgroundColor":"#fff"})
  })  
  
  $("#new_item").on('click',function(){
    var item="";
    var last_sn = $(".charge_item:last").find('.sn').text();
    console.log(last_sn);
    item +='<tr class="charge_item">';
    item +='<td >'+(last_sn*1+1)+'</td>';
    item +='<td><input type="number" class="money form-control" value="0"  min="1" /></td>';
    item +='<td><input type="number" class="gift form-control" value="0"  min="1" /></td>';
    item +='</tr>';
    $("table").append(item);

  })

  $("#save").on('click',function(){
    var charge_setup=[];

    $(".charge_item").each(function(){
      var setup=[];
      var money=$(this).find('.money').val();
      var gift=$(this).find('.gift').val();

      if(isNaN(money) || money=='' || money<1){
        layer.msg('无效的金额设置！'+money);
        $(this).find('.money').focus();
        return false;
      }

      if(isNaN(gift) || gift=='' || gift<1){
        layer.msg('无效的赠送金额设置！'+gift);
        $(this).find('.gift').focus();
        return false;
      }

      charge_setup.push({money:money,gift:gift});
    })

    console.log(charge_setup);

    //询问框
    layer.confirm('无效项目自动忽略，是否确定保存充值配置?', {btn: ['Yes','No'],title:'Confirm' }, function(){
      $('form').submit();

      $.ajax({
        url:'/index.php/customer/charge_setup',
        data:{'charge_setup':charge_setup},
        success:function(res){
          if(res.code){
            layer.msg(res.msg);
            window.location.reload();
          }else{
            layer.msg(res.msg);
          }
        }
      })
    }, function(){
      layer.msg("取消操作!");
      return 0;
    });

  })

})


</script>


</body>
</html>
